package com.haofengsoft.scce.adapter.mine;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.haofengsoft.scce.R;

public class ConsultationCollectionAdapter extends BaseAdapter {

	private Context context;
	private List<ConsultationCollectModel> consultationCollectModelList;
	private ConsultationCollectionInterface consultationCollectionInterface;
	private boolean isEdit = false;

	@SuppressLint("UseSparseArrays")
	private Map<Integer, Boolean> isCheckMap = new HashMap<Integer, Boolean>();

	public ConsultationCollectionAdapter(Context context, List<ConsultationCollectModel> consultationCollectModelList,
			ConsultationCollectionInterface consultationCollectionInterface) {
		this.context = context;
		this.consultationCollectModelList = consultationCollectModelList;
		this.consultationCollectionInterface = consultationCollectionInterface;
		configCheckMap(false);
	}

	/**
	 * 首先,默认情况下,所有项目都是没有选中的.这里进行初始化
	 */
	public void configCheckMap(boolean isChecked) {
		isCheckMap.clear();
		for (int i = 0; i < consultationCollectModelList.size(); i++) {
			isCheckMap.put(i, isChecked);
		}
	}

	@Override
	public int getCount() {
		return this.consultationCollectModelList.size();
	}

	@Override
	public Object getItem(int position) {
		return this.consultationCollectModelList.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	public void addConsultationCollectList(List<ConsultationCollectModel> consultationCollectModelList) {
		this.consultationCollectModelList.addAll(consultationCollectModelList);
	}

	@Override
	public View getView(int position, View convertview, ViewGroup parent) {
		View view = null;
		if (convertview == null) {
			view = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.mine_consultation_collection_item, parent,
					false);
		} else {
			view = (ViewGroup) convertview;
		}
		ConsultationCollectModel consultationCollectModel = (ConsultationCollectModel) getItem(position);
		TextView titleTv = (TextView) view.findViewById(R.id.consultation_collection_item_title_tv);
		titleTv.setText(consultationCollectModel.getTitle());
		TextView sourceTv = (TextView) view.findViewById(R.id.consultation_collection_item_source_tv);
		sourceTv.setText(consultationCollectModel.getSource());
		TextView timeTv = (TextView) view.findViewById(R.id.consultation_collection_item_time_tv);
		timeTv.setText(consultationCollectModel.getTime());
		CheckBox editCb = (CheckBox) view.findViewById(R.id.consultation_collection_item_edit_cb);
		if (isEdit) {
			editCb.setVisibility(View.VISIBLE);
		} else {
			editCb.setVisibility(View.GONE);
		}
		editCb.setChecked(isCheckMap.get(position));

		editCb.setOnCheckedChangeListener(new EditCheckChangeListener(position));

		return view;
	}

	class EditCheckChangeListener implements OnCheckedChangeListener {

		private int position;

		public EditCheckChangeListener(int position) {
			this.position = position;
		}

		@Override
		public void onCheckedChanged(CompoundButton checkedButton, boolean isChecked) {
			isCheckMap.put(position, isChecked);
			consultationCollectionInterface.checkChange();

		}

	}

	public void setEdit(boolean isEdit) {
		this.isEdit = isEdit;
	}

	public void remove(int position) {
		this.consultationCollectModelList.remove(position);
	}

	public Map<Integer, Boolean> getCheckMap() {
		return this.isCheckMap;
	}

	public int gainCheckedSum() {
		int sum = 0;
		for (int i = 0; i < consultationCollectModelList.size(); i++) {
			if (isCheckMap.get(i)) {
				sum += 1;
			}
		}
		return sum;
	}

	public void delItem() {
		// 获取当前的数据数量
		int count = consultationCollectModelList.size();
		// 进行遍历
		for (int i = 0; i < count; i++) {
			// 因为List的特性,删除了2个item,则3变成2,所以这里要进行这样的换算,才能拿到删除后真正的position
			int position = i - (count - consultationCollectModelList.size());
			if (isCheckMap.get(i) != null && isCheckMap.get(i)) {
				remove(position);
			}
		}
		configCheckMap(false);
		consultationCollectionInterface.checkChange();
	}
}
